package com.bcw.springtest.bean;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Component
public class MyBean implements CommandLineRunner {

    private ApplicationArguments applicationArguments;
    @Value("${app.desc}")
    private String appDesc;

    @Value("${age}")
    private Integer age;

    @Value("${name}")
    private String name;
    @Value("${my.secret}")
    private String secret;
    @Value("${my.number}")
    private String number;


    @Autowired
    public MyBean(ApplicationArguments args) {

        this.applicationArguments = args;
        boolean debug = args.containsOption("debug");
        List<String> files = args.getNonOptionArgs();
        // if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
    }

    public ApplicationArguments getApplicationArguments() {
        return applicationArguments;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("SpringApplication.run()之前执行点啥123123u1925u8023945839012890");
    }


    public static void main(String[] args) throws JsonProcessingException {
        JsonFactory factory = new JsonFactory();
// configure, if necessary:
        factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
        ObjectMapper mapper =new ObjectMapper();
        Map<String, String> map = new HashMap<String, String>(){{
                put("a", "1");
                put("b", "122");
        }};

        String mapJson = mapper.writeValueAsString(map);
        System.out.println(mapJson);

        List<Map<String, String>> list = new ArrayList<>();
        list.add(map);
        list.add(map);

        String listJson = mapper.writeValueAsString(list);
        System.out.println(listJson);

        Map<String, String> reMap = mapper.readValue(mapJson, HashMap.class);
        List<Map<String, String>> reList = mapper.readValue(listJson, ArrayList.class);
        System.out.println(reList);

    }

}

